//
//  CrseAsstApp.swift
//  CrseAsst
//
//  Created by 申铭 on 2024/2/22.
//

import SwiftUI

@main
struct CrseAsstApp: App {
    let persistenceController = PersistenceController.shared
    
    init() {
        log.verbose(NSHomeDirectory())
        setupApperance()
    }

    var body: some Scene {
        WindowGroup {
            RootView()
//                .onAppear(perform:setupAppearance)
//                .onAppear(perform:setupStatusBarStyle)
//            ContentView()
//                .environment(\.managedObjectContext, persistenceController.container.viewContext)
        }
    }
    
    private func tabbarItem(text: String, image: String) -> some View {
        VStack {
            Image(systemName: image)
                .imageScale(.large)
            Text(text)
        }
    }
    
    private func setupApperance() {
        print("setupApperance")
        UINavigationBar.appearance().largeTitleTextAttributes = [
            NSAttributedString.Key.foregroundColor: UIColor(named: "myblue")!]
        
        UINavigationBar.appearance().titleTextAttributes = [
            NSAttributedString.Key.foregroundColor: UIColor(named: "myblue")!]
        
        UIBarButtonItem.appearance().setTitleTextAttributes([
            NSAttributedString.Key.foregroundColor: UIColor(named: "myblue")!,
        ], for: .normal)
        
        UIWindow.appearance().tintColor = UIColor(named: "myblue")
        
        ETKeyboardHandler.shared.setup()
    }
    
    func setupAppearance() {
        print("setupAppearance")
//        let backImage = UIImage(named: "back")?
//                .withPadding(.init(top: -2, left: 0, bottom: 0, right: -4))
//        
//        // 统一导航栏样式
//        UINavigationBar.appearance().backIndicatorImage = backImage
//        UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
//        UINavigationBar.appearance().shadowImage = UIImage()
//        UINavigationBar.appearance().tintColor = UIColor(named: "navigation_tint")
//        UINavigationBar.appearance().barTintColor = UIColor(named: "navigation_bar_tint")
        
//        if #available(iOS 15.0, *) {
//            // 避免 iOS15 的默认行为导致 NavigationBar 没有背景色
//            let navigationBarAppearance = UINavigationBarAppearance()
//            navigationBarAppearance.configureWithDefaultBackground()
//            UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
//            
//            // 避免 iOS15 的默认行为导致 TabBar 没有背景色
//            let tabBarAppearance = UITabBarAppearance()
//            tabBarAppearance.configureWithDefaultBackground()
//            UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
//            
//            // 避免 iOS15 增加的列表顶部空白
//            UITableView.appearance().sectionHeaderTopPadding = 0
//        }
    }
    
    func setupStatusBarStyle() {
        print("setupStatusBarStyle")
//        guard
//            let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first,
//            let statusBarManager = window.windowScene?.statusBarManager
//            else { return }
//        
//        StatusBarStyle.Key.defaultValue.getter = {
//            statusBarManager.statusBarStyle
//        }
//        StatusBarStyle.Key.defaultValue.setter = {
//            /// 暂时还没有找到更好的方法
//            UIApplication.shared.statusBarStyle = $0
//        }
    }
}
